var app = new Vue({
	el: '#app',
	data: {
		order: [],
		customerFile:[],
		serviceFile:[],
		spare:'',
		first:true,
		openId: ''
    },
    mounted: function () {
		this.$nextTick(function () {
            openId = $.cookie('openId');
            var id = getUrlParam('id');
			Server.getFromCustomer('/searchRepairDetailById', {id: id}, function (result) {
				app.order = result.data.repair;
				app.spare = result.data.spare.join('<br>');
				app.customerFile = result.data.customerFile;
				app.serviceFile = result.data.serviceFile;
                console.log(app.spare);
			});
		});
	},
	updated(){
		//初始加载时初始化video控件,且工单状态在服务中,不为取消服务
		if(app.first && app.order.repairState > REPAIR_STATE_AUDIT && app.order.repairState !== REPAIR_STATE_CANCEL){
			for(var i in app.customerFile){
				if(app.customerFile[i].fileType === REPAIR_FILE_TYPE_VIDEO){
					app.getFiles(app.customerFile[i].fileId);
				}
			}
			for(var i in app.serviceFile){
				if(app.serviceFile[i].fileType === REPAIR_FILE_TYPE_VIDEO){
					console.log(app.serviceFile[i]);
					app.getFiles(app.serviceFile[i].fileId);
				}
			}
			app.first = false;
		}
	},
    methods:{
        getRepairState: function (order) {
			switch (order.repairState) {
				case REPAIR_STATE_SUBMIT:
					return {
						state: "header1",
						text: '提交成功，等待审核',
						html: '<button class="mui-btn" onclick="app.cancelRepair('+order.id +','+ order.workOrderNo+')">取消报修</button>'
					};
				case REPAIR_STATE_AUDIT:{
					return {
						state: "header2",
						text: '审核通过，等待预约上门',
						html: '<button class="mui-btn confirmBtn" onclick="app.callToServices()">联系我们</button>'
					};
				}
				case REPAIR_STATE_BESPEAK:
					return {
						state: "header2",
						text: '预约成功，等待上门',
						html: '<button class="mui-btn confirmBtn" onclick="app.callToServices()">联系我们</button>'
					};
				case REPAIR_STATE_SIGN:
					return {
						state: "header4",
						text: '服务完成',
						html: '<a href="comment.html?id='+ order.id+'" class="mui-btn confirmBtn" onclick="app.callToServices()">立即评价</a>'
					};
				case REPAIR_STATE_CANCEL:
					return {
						state: "",
						text: '服务取消',
						html: ''	
					};
				default:
					return {
						state: "header1",
						text: '提交成功，等待审核',
						html: '<button class="mui-btn" onclick="app.cancelRepair('+order.id +','+ order.workOrderNo+')">取消报修</button>'
					};
			}
        },
        getOrderStep:function(state){
            switch (state) {
                case REPAIR_STATE_SUBMIT:
					return '<img src="../img/img27.png">';
				case REPAIR_STATE_AUDIT:{
					return '<img src="../img/img28.png">';
				}
				case REPAIR_STATE_BESPEAK:
					return '<img src="../img/img28.png">';
				case REPAIR_STATE_SIGN:
                    return '<img src="../img/img30.png">';
                case REPAIR_STATE_CANCEL:
                    return false;
				default:
					return '<img src="../img/img29.png">';
            }
		},
		cancelRepair:function(id,workOrderNo){
			/**
			 * 取消报修服务
			 * @param {Integer} id 报修id
			 * @param {String} workOrderNo 报修工单
			 */
			mui.confirm('是否取消工单['+workOrderNo+']', function(e){
				console.log(e);
				
				if(e.index == 1){
					console.log(231);
					
					Server.postToCustomer('/cancelRepair', {id: id}, function(result){
						if(result.code === 100){
							mui.toast("取消成功!");
							app.order.repairState = REPAIR_STATE_CANCEL;
						}else{
							mui.toast('操作失败渲!');
						}
					});
				}
			});
		},
		callToServices:function(){
			console.log(2341);
			mui.confirm('是否拨打4008889880', function (e) {
				if (e.index != 0) {
					var tel = $('<a href="tel:4008889880"></a>');
					tel[0].click();
				}
			})
		},
		getRepairMethod:function(type){
			/**
			 * 判断维修方式
			 * @param {Integer} type 维修类型
			 * @return 维修名称
			 */
			if(type === REPAIR_METHOD_SCENCE){
				return "现场维修";
			}else{
				return "寄件维修";
			}
		},
		getFiles:function(fileID){
			var player = TCPlayer('f'+fileID, { // player-container-id 为播放器容器ID，必须与html中一致
                fileID: fileID, // 请传入需要播放的视频filID 必须
                appID: "1256187897", // 请传入点播账号的appID 必须
                autoplay: false //是否自动播放
                //其他参数请在开发文档中查看
			});
		},
		previewImage:function(){
			var $gallery = $("#gallery"),
			$galleryImg = $("#galleryImg");
			$galleryImg.attr("src", event.target.getAttribute("src"));
			$gallery.fadeIn(100);
			$gallery.on("click", function () {
				$gallery.fadeOut(100);
			});
			//删除图片 删除图片的代码也贴出来。
			$(".weui-gallery__del").click(function () {
				console.log('删除');
				$("#picsPlace").find("div").eq(index).remove();
			});
		}
    }
});